import Vue from "vue";
import VueRouter from "vue-router";
import Main from "../views/Main.vue";
import CategoryEdit from "../views/CategoryEdit.vue";
import CategoryList from "../views/CategoryList.vue";
import EquipAddEdit from "../views/equip/EquipEdit.vue";
import EquipList from "../views/equip/EquipList.vue";
import HeroAddEdit from "../views/heroes/HeroAddEdit.vue";
import HeroList from "../views/heroes/HeroList.vue";
import ArticleAddEdit from "../views/articles/ArticleAddEdit.vue";
import ArticleList from "../views/articles/ArticleList.vue";
import AdvertisesAddEdit from "../views/advertises/AdvertiseAddEdit.vue";
import AdvertisesList from "../views/advertises/AdvertisesList.vue";
import AdminUserAddEdit from "../views/admin_users/AdminUserAddEdit.vue";
import AdminUserList from "../views/admin_users/AdminUserList.vue";
import Login from "../views/login/Login.vue"; // 登录路由

Vue.use(VueRouter);

const routes = [
  // 登录路由
  {
    path: "/login",
    name: "login",
    component: Login,
    meta: { isPublic: true },
  },
  {
    path: "/",
    name: "main",
    component: Main,
    children: [
      // 新闻路由
      {
        path: "/categories/create",
        name: "category",
        component: CategoryEdit,
      },
      // 添加props属性可以不用在组件中使用prop参数，不用this.$route.params.id，解耦
      {
        path: "/categories/edit/:id",
        component: CategoryEdit,
        props: true,
      },
      {
        path: "/categories/list",
        name: "categoryList",
        component: CategoryList,
      },
      // 装备路由
      {
        path: "/equip/create",
        component: EquipAddEdit,
      },
      // 添加props属性可以不用在组件中使用prop参数，不用this.$route.params.id，解耦
      {
        path: "/equip/edit/:id",
        component: EquipAddEdit,
        props: true,
      },
      {
        path: "/equip/list",
        component: EquipList,
      },
      // 英雄路由
      {
        path: "/hero/create",
        component: HeroAddEdit,
      },
      // 添加props属性可以不用在组件中使用prop参数，不用this.$route.params.id，解耦
      {
        path: "/hero/edit/:id",
        component: HeroAddEdit,
        props: true,
      },
      {
        path: "/hero/list",
        component: HeroList,
      },
      // 文章路由
      {
        path: "/articles/create",
        component: ArticleAddEdit,
      },
      // 添加props属性可以不用在组件中使用prop参数，不用this.$route.params.id，解耦
      {
        path: "/articles/edit/:id",
        component: ArticleAddEdit,
        props: true,
      },
      {
        path: "/articles/list",
        component: ArticleList,
      },
      // 广告路由
      {
        path: "/advertises/create",
        component: AdvertisesAddEdit,
      },
      // 添加props属性可以不用在组件中使用prop参数，不用this.$route.params.id，解耦
      {
        path: "/advertises/edit/:id",
        component: AdvertisesAddEdit,
        props: true,
      },
      {
        path: "/advertises/list",
        component: AdvertisesList,
      },
      // 管理员路由
      {
        path: "/admin_users/create",
        component: AdminUserAddEdit,
      },
      // 添加props属性可以不用在组件中使用prop参数，不用this.$route.params.id，解耦
      {
        path: "/admin_users/edit/:id",
        component: AdminUserAddEdit,
        props: true,
      },
      {
        path: "/admin_users/list",
        component: AdminUserList,
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !sessionStorage.token) {
    next("/login");
  }
  next();
});

export default router;
